#!/bin/bash
# author: honway.liu
# email: liu.hongwei@redstartclouds.com
# desc: this script is used to push the video stream of the robot camera to Alibaba
# through the RTMP protocol, Because there is URL protection when pushing the stream,
# the authentication key needs to be calculated according to the key, The calculation
# rule is md5(path+expr time + rand number + uid + key)

robotNo=robot42
validSec=3600
uid=0
rand=0
key=aOsIgzXw3VajPyg6
expire=$(date +%s+$validSec | bc)
pushStreamUrl=rtmp://pushvideo-robot.aiyunshen.com/live/${robotNo}
# shellcheck disable=SC2001
urlPath=$(echo $pushStreamUrl | sed 's#rtmp://\([^/]*\)\(.*\)#\2#')
md5str=$(echo -n "$urlPath"-"$expire"-$rand-$uid-$key | md5sum | awk '{print $1}')
authKey=${expire}-${rand}-${uid}-${md5str}
rtmpServer=$pushStreamUrl"?auth_key="$authKey
rosTopic=/camera/color/image_raw
rosParam="-pix_fmt yuv420p -f flv -vcodec libx264 -s 640*480"
echo "RTMP:" "$rtmpServer"

if pgrep ffmpeg; then
    echo "ffmpeg is running"
else
    echo "ffmpeg is not running"
fi
ffmpeg -f mjpeg -i http://192.168.50.51:8080/stream?topic=$rosTopic "$rosParam" $pushStreamUrl